﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using PQT.API;
using PQT.Common;
using PQT.Controls;
using PQT.API.File;
using System.Data;
using System.Data.SqlClient;
using cbmobile.BLC;

namespace cbmobile.UserControl
{
    public partial class TinTuc_List : System.Web.UI.UserControl
    {
        int pagesize = 20;
        BLC_TinTuc nBLC = new BLC_TinTuc();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindGirdView();
            }
        }

        protected string GetStatusName(object p_status)
        {
            if (p_status == DBNull.Value)
                return "";
            int i = Convert.ToInt32(p_status);
            string status;
            if (i == 1)
            {
                status = "True";
            }
            else
            {
                status = "Fasle";
            }
            return status;
        }

        private int CurrentPage
        {
            get
            {
                if (ViewState["g_CurrentPage"] != null)
                    return Convert.ToInt32(ViewState["g_CurrentPage"]);
                return 1;
            }
            set
            {
                ViewState["g_CurrentPage"] = value;
            }
        }

        private void BindGirdView()
        {
            int status = Convert.ToInt32(ddlStatus.SelectedValue);
            int searchtype = Convert.ToInt32(ddlSearchType.SelectedValue);
            int sortOption = Convert.ToInt32(ddlSearchType.SelectedValue);
            DataTable dt = nBLC.Rows_NewsByListCategory(1, txtSearchText.Text);
            for (int i = dt.Rows.Count; i < pagesize; i++)
            {
                dt.Rows.Add(dt.NewRow());
            }
            gvList.DataSource = dt;
            gvList.DataBind();

            int total = nBLC.GetTotalRecord(txtSearchText.Text);

            PQTPager1.RecordCount = total;

            PQTPager1.PageSize = pagesize;
            PQTPager1.PagerButtonCount = 5;

            PQTPager1.CurrentPageIndex = this.CurrentPage - 1;
        }

        private bool DeleteNews(int p_ID)
        {
            try
            {
                nBLC.DeleteNews(p_ID);
                return true;
            }
            catch (System.Exception e)
            {
                return false;
            }
        }

        protected void btnDelete_Click(object sender, EventArgs e)
        {
            try
            {
                string[] arrIDs = hdnIDs.Value.TrimEnd('|').Split('|');
                foreach (string id in arrIDs)
                {
                    int idP = Convert.ToInt32(id);
                    DeleteNews(idP);
                }
                Alert.Show("Xóa thành công");
                BindGirdView();
            }
            catch (System.Exception ex)
            {
                Alert.Show("Xóa thất bại");
            }
        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            BindGirdView();
        }

        //protected NewControl ParentCtrl()
        //{
        //    Control objParent = Parent;
        //    while(!(objParent is NewControl))
        //    {
        //        objParent =objParent.Parent;
        //    }
        //    return objParent as NewControl;
        //}

        protected void lnkLink_Click(object sender, EventArgs e)
        {
            
            
             //int.Parse(e.CommandArgument.ToString());
        }

        protected void gvList_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "edit")
            {
                Response.Redirect(string.Format("/RenderMain.aspx?muid=TinTuc_Entry&action=Update&newId={0}",e.CommandArgument));
            }
            //nBLC.ID_News =  int.Parse(e.CommandArgument.ToString());
            //ParentCtrl().SetViewState("g_ID_News", e.CommandArgument);
        }

    }
}